<?php 
require 'db_connect.php';

	function getDati($Nome){
		$found=1;
		$qry = "SELECT Utente, email, nome, cognome, sesso, DATE_FORMAT(nascita, '%d/%m/%Y') as nascita, citta, avatar FROM Utenti
                                WHERE Utente='".$Nome."'";
		$check = mysql_query($qry) or $found=0;
		if (!$found) return;
		$array = mysql_fetch_assoc($check);
		
		return $array;
	}

	function generaTabella($check,$p=1,$pag) {
		echo '<table id="tabella">';
      	$array = mysql_fetch_assoc($check);
      	if($array){
			echo '<tr>';
			foreach($array as $key=>$dato){
				$un = !isset($_GET['un']) || $_GET['un'] ? '&amp;un=0' : '&amp;un=1';
				echo '<td class="tabutentititle" title="'.$key.'"><a href="'.$_SERVER['PHP_SELF'].'?p='.$p.'&amp;pag='.$pag.'&amp;o='.$key.$un.'">'.$key.'</a></td>';
			}
			echo '<td class="tabutentititle">Seleziona</td></tr>';
			while($array) {				
				echo '<tr>';					
				foreach($array as $dato){
					$dato = htmlentities($dato);
					$utente = cleanSimpleString($array['Utente']);
					echo "<td class=\"tabutenti\" title=\"$dato\"><a href=\"pannello.php?u=$utente&p=$p\">$dato</a></td>";
				}
				echo '<td class="tabutenti"><input type="checkbox"  name="'.$array['Utente'].'"></tr>';
				$array = mysql_fetch_assoc($check); //next...
			} 
		}
		echo '</table>';
	}	
	
	function generaTabellaMessaggi($check, $array, $pag){
		
		echo '<table id="tabella">';
      	$array = mysql_fetch_assoc($check);
      	if($array){
			echo '<tr>';
			
			$p = isset($_GET['p']) ? '?p='.cleanNumber($_GET['p'],1)  : '?p=1';
			
			foreach($array as $key=>$dato){
				
				if($key!='Id') {
					// u = 1 - ordine corretto DESC- se u=0 -ordine inverso ASC
					$u = !isset($_GET['u']) || $_GET['u'] ? '&u=0' : '&u=1';
					echo '<td class="tabutentititle" title="'.$key.'"><a href="'.$_SERVER['PHP_SELF'].$p.'&amp;pag='.$pag.'&amp;o='.$key.$u.'">'.$key.'</a></td>';
					
				}
			}
			echo '<td class="tabutentititle">Seleziona</td></tr>';
			while($array) {				
				echo '<tr>';					
				foreach($array as $key=>$dato){
					$dato = htmlentities($dato);
					if ($key!='Id') echo '<td class="tabutenti" title="'.$dato.'"><a href="posta.php?Id='.$array['Id'].'">'.$dato.'</a></td>';
				}
				echo '<td class="tabutenti"><input type="checkbox"  name="'.$array['Id'].'"></tr>';
				$array = mysql_fetch_assoc($check); //next...
			} 
		}
		echo '</table>';
	}

	function randPass(){
		$d='';
		for ($i=0; $i<8; $i++) {
		    $e=rand(1,30)%2;
		    $d.= $e ? chr(rand(65,90)) : chr(rand(48,57));
		}
		return $d;
	}
	
	function cleanNumber($n, $default=0){
		return (preg_match("/^[0-9]{1,40}$/",$n)) ? $n : $default;
	}
	
	function cleanSimpleString($s, $maxSize=30){		
		//$s = preg_replace("/[']/", $default, $s);//sostituisci tutto quello che non è alfanumerico
		$s = str_replace("'"," ", $s);
		$s = str_replace("\""," ", $s);
		$s = str_replace("\""," ", $s);
		$s = str_replace("<"," ", $s);
		$s = str_replace(">"," ", $s);
		$s = str_replace("\\"," ", $s);
		$s = substr($s,0,$maxSize);
		return $s;
	}
	
	function cleanString($s, $maxSize=300, $exception=''){
		$s = strip_tags($s, $exception);
		$s = htmlspecialchars($s);
		
		$s = substr($s, 0, $maxSize);
		$s = addslashes($s);
		return $s;
	}
	
	function okDate($s){// dd/mm/yyyy
		$s=substr($s,0,10);
		$n = explode('/',$s);
		if ( checkdate($n[1],$n[1],$n[2])) return $s; else return 0;
		
	}
	
	
	function okEmail($e){
		$e = substr($e,0,50);
		if (preg_match("/^.+[@].+$/",$e)) return $e; else return 0;
	}

	function controllaTipo($tipo) {
		$tipi_immagine = array("image/jpeg", "image/gif", "image/png");
		
		foreach ($tipi_immagine as $formato)
		if (strpos($tipo, $formato)===0)
			return $formato;
		return FALSE;
	}	

	function controllaFormato($nomefile) {
		$formati_immagine = array(".jpg", ".gif", ".png");
		foreach ($formati_immagine as $formato)
		if (strrpos($nomefile, $formato))
			return $formato;
		return FALSE;
	}

	function ridimensiona_immagine($src_file) {
		list($src_w, $src_h, $formato) = getimagesize($src_file);
		switch ($formato) {
		        case 1: $src_image = imagecreatefromgif($src_file); break;
		        case 2: $src_image = imagecreatefromjpeg($src_file);  break;
		        case 3: $src_image = imagecreatefrompng($src_file); break;
		}
		if($src_w >= $src_h) {
			$dst_w = 128;
			$dst_h = ceil(128*($src_h/$src_w));
		}
		else {
			$dst_h = 128;
			$dst_w = ceil(128*($src_w/$src_h));
		}
		$dst_image = imagecreatetruecolor($dst_w, $dst_h);
		if(($formato == 1) || ($formato == 3)) { 	//Mantieni lo sfondo trasparente di gif e png
		        imagealphablending($dst_image, false);
		        imagesavealpha($dst_image, true);
		        $transparent = imagecolorallocatealpha($dst_image, 0, 0, 0, 127);
		        imagefilledrectangle($dst_image, 0, 0, $dst_w, $dst_h, $transparent);
		}
		$src_x = 0;
		$src_y = 0;
		$dst_x = 0;
		$dst_y = 0;

		if(imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)) {
			switch ($formato) {
				case 1: imagegif($dst_image, $src_file);break;
				case 2: imagejpeg($dst_image, $src_file);break;
				case 3: imagepng($dst_image, $src_file);break;
			}
		}
		else
			echo 'fallito';

	}

?>
